﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace EFong.Contract
{
    [DataContract]
    public class FlightReturn : Order
    {
        public FlightReturn() 
        {
            OrderType = "飞机票退票";
        }

        private decimal _vendorUsedPrice;
        [DataMember]
        public decimal VendorUsedPrice
        {
            get { return _vendorUsedPrice; }
            set { _vendorUsedPrice = value; }
        }

        private decimal _vendorReturnPrice;
        [DataMember]
        public decimal VendorReturnPrice
        {
            get { return _vendorReturnPrice; }
            set { _vendorReturnPrice = value; }
        }

        private decimal _usedTax;
        [DataMember]
        public decimal UsedTax
        {
            get { return _usedTax; }
            set { _usedTax = value; }
        }

        private Currency _vendorCurrency;
        [DataMember]
        public Currency VendorCurrency
        {
            get { return _vendorCurrency; }
            set { _vendorCurrency = value; }
        }

        private Currency _currency;
        [DataMember]
        public Currency Currency
        {
            get { return _currency; }
            set { _currency = value; }
        }

        private decimal _customerUsedPrice;
        [DataMember]
        public decimal CustomerUsedPrice
        {
            get { return _customerUsedPrice; }
            set { _customerUsedPrice = value; }
        }

        private decimal _customerReturnPrice;
        [DataMember]
        public decimal CustomerReturnPrice
        {
            get { return _customerReturnPrice; }
            set { _customerReturnPrice = value; }
        }

        private decimal _exchangeRate = 1;
        [DataMember]
        public decimal ExchangeRate
        {
            get { return _exchangeRate; }
            set { _exchangeRate = value; }
        }

        /// <summary>
        /// 利润
        /// </summary>
        public decimal? Profit
        {
            get 
            {
                if (this.FlightOrderDetail == null)
                    return null;

                if (this.Currency != this.VendorCurrency) 
                {
                    if (this.ExchangeRate == 0)
                        return null;
                    else
                        return CustomerReturnAmount - VendorReturnAmount * ExchangeRate;
                }
                else    
                    return CustomerReturnAmount - VendorReturnAmount ;
            }
        }

        /// <summary>
        /// 扣减的积分
        /// </summary>
        public decimal? Intergral 
        {
            get 
            {
                return GetIntergral(FlightOrderDetail.FlightOrder.Customer);
            }
        }

        public decimal? GetIntergral(Customer customer)
        {

                if (this.FlightOrderDetail == null)
                    return null;
                return (FlightOrderDetail.AccountReceivable - FlightOrderDetail.Tax1 - FlightOrderDetail.Tax2 - CustomerUsedPrice) * customer.FlightIntergralRate;
        }

        /// <summary>
        /// 应退
        /// </summary>
        public decimal? CustomerReturnAmount
        {
            get 
            {
                if (this.FlightOrderDetail == null)
                    return null;

                return 0 - (FlightOrderDetail.AccountReceivable - CustomerUsedPrice - UsedTax - CustomerReturnPrice);
            }
        }

        /// <summary>
        /// 应收退款
        /// </summary>
        public decimal? VendorReturnAmount
        {
            get
            {
                if (this.FlightOrderDetail == null)
                    return null;
                return 0 - (FlightOrderDetail.AccountPayable - VendorUsedPrice - VendorReturnPrice);
            }
        }

        private Vendor _vendor;
        [DataMember]
        public Vendor Vendor
        {
            get { return _vendor; }
            set { _vendor = value; }
        }

        private Customer _customer;
        [DataMember]
        public Customer Customer
        {
            get { return _customer; }
            set { _customer = value; }
        }

        private BankAccount _bankAccount;
        [DataMember]
        public BankAccount BankAccount
        {
            get { return _bankAccount; }
            set { _bankAccount = value; }
        }

        private string _paymentType = string.Empty;
        [DataMember]
        public string PaymentType
        {
            get { return _paymentType; }
            set { _paymentType = value; }
        }

        private FlightOrderDatail _flightOrderDatail;
        //[DataMember]
        public FlightOrderDatail FlightOrderDetail
        {
            get { return _flightOrderDatail; }
            set { _flightOrderDatail = value; }
        }

        public override bool Equals(object obj)
        {
            FlightReturn temp = obj as FlightReturn;
            if (temp == null)
                return false;
            return (this.ID.Equals(temp.ID));
        }


     
    }
}
